package com.lp.biz.operations.service.impl;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.lp.biz.content.mapper.BizCoreContentMapper;
import com.lp.biz.content.model.entity.BizCoreContent;
import com.lp.biz.operations.model.entity.BizRecommendToday;
import com.lp.biz.operations.mapper.BizRecommendTodayMapper;
import com.lp.biz.operations.model.vo.TodayAdd;
import com.lp.biz.operations.service.BizRecommendTodayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lp.common.component.RedisService;
import com.lp.common.entity.PageParams;
import com.lp.common.exception.BaseException;
import com.lp.common.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.lp.common.constant.CacheConstant.BIZ_HOME_KEY;
import static com.lp.common.constant.CategoryConstant.CODE_READING;
import static com.lp.common.constant.CategoryConstant.CODE_STORY;
import static com.lp.common.constant.CommonConstant.NEGATE;
import static com.lp.common.constant.CommonConstant.SURE;

/**
 * @program: backend
 * @description: BizRecommendToday 服务实现类
 * @author Ke.Song
 * @since 2024-07-14 23:26:00
 */
@Service
public class BizRecommendTodayServiceImpl extends ServiceImpl<BizRecommendTodayMapper, BizRecommendToday> implements BizRecommendTodayService {

    @Autowired
    private RedisService redis;

    @Autowired
    private BizCoreContentMapper coreContentMapper;

    @Override
    public CommonResult getTodayList(PageParams query) {
        PageHelper.startPage(query.getPage(), query.getSize());
        return CommonResult.restPage(baseMapper.getTodayList());
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addOrUpdateToday(TodayAdd params) {
        BizCoreContent content = coreContentMapper.selectById(params.getContentId());
        if (content == null || content.getIsDeleted().equals(SURE) || content.getIsDisplay().equals(NEGATE)) {
            throw new BaseException("内容选择错误");
        } else if (!content.getBizCategoryCode().equals(CODE_STORY) && !content.getBizCategoryCode().equals(CODE_READING)) {
            throw new BaseException("内容选择错误");
        }
        BizRecommendToday one = getOne(new QueryWrapper<BizRecommendToday>().lambda()
                .eq(BizRecommendToday::getDisplayAt, params.getDisplayAt()).eq(BizRecommendToday::getIsDelete, NEGATE));
        if (params.getId() == null) {
            if (one != null) {
                throw new BaseException("展示时间选择错误");
            }
            one = new BizRecommendToday();
            one.setBizContentId(content.getId());
            one.setBizCategoryId(content.getBizCategoryId());
            one.setBizCategoryCode(content.getBizCategoryCode());
            one.setDisplayAt(params.getDisplayAt());
            one.setCreateAt(LocalDateTime.now());
            save(one);
        } else {
            BizRecommendToday today = getById(params.getId());
            if (today == null || today.getIsDelete().equals(SURE)) {
                throw new BaseException("今日推荐输入错误");
            }
            if (one != null && !one.getId().equals(params.getId())) {
                throw new BaseException("展示时间选择错误");
            }
            today.setBizCategoryId(content.getBizCategoryId());
            today.setBizCategoryCode(content.getBizCategoryCode());
            today.setBizContentId(content.getId());
            today.setCreateAt(LocalDateTime.now());
            today.setDisplayAt(params.getDisplayAt());
            updateById(today);
        }
        redis.delete(BIZ_HOME_KEY);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteTodayRecommend(Integer recommendId) {
        BizRecommendToday today = getById(recommendId);
        if (today == null || today.getIsDelete().equals(SURE)) {
            throw new BaseException("今日推荐输入错误");
        }
        today.setIsDelete(SURE);
        updateById(today);
        redis.delete(BIZ_HOME_KEY);
    }
}
